class Solution {
public:
    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
        vector<vector<int>> adj(n);
        for (auto& e : edges) {
            adj[e[0]].push_back(e[1]);
            adj[e[1]].push_back(e[0]);
        }
        function<int(int, int)> dfs = [&](int node, int parent) {
            int time = 0;
            for (int child : adj[node]) {
                if (child == parent) continue;
                int childTime = dfs(child, node);
                if (childTime > 0 || hasApple[child]) {
                    time += childTime + 2;
                }
            }
            return time;
        };
        return dfs(0, -1);
    }
};